package nige.first;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
 * From : http://javalessons.com/cgi-bin/jme/j2me-tutorial-java.cgi?1cd=cnv&sid=ao789
 * @author ngenevaux
 *
 */
public class Les4
    extends MIDlet 
    implements CommandListener {
  private Display dsp;
  private Form fm;
  private Displayable curr;
  private TextField tf1, tf2;
  private StringItem sitres;
  private long result;
  private Command cmdq, cmdcalc ;
  private CusCanvas canv;
   
  public Les4() {
    fm = new Form("Calculation");
    tf1 = new TextField(null, "12300000", 8, TextField.NUMERIC );
    tf2 = new TextField(null, "24600000", 8, TextField.NUMERIC );
    sitres = new StringItem("Result", "");
    cmdq = new Command("Quit", Command.EXIT, 0);
    cmdcalc = new Command("Add", Command.SCREEN, 0);

    fm.append(tf1);
    fm.append(tf2);
    fm.append(sitres);
    fm.addCommand(cmdcalc);
    fm.addCommand(cmdq);
    fm.setCommandListener(this);
  }
  int n1=0, n2=0 ;  
  private void calc()
  {
      try {
        n1 = Integer.parseInt( tf1.getString() );
        n2 = Integer.parseInt( tf2.getString() );
        result = n1 + n2 ;
        sitres.setText( result + "" );
      } catch (NumberFormatException e)
        { sitres.setText("unknown");
//          e.printStackTrace();
        }
  }
  public void commandAction(Command c, Displayable s) {
        if (c == cmdq){
          notifyDestroyed();
          return;
        }
        calc();
        if (canv==null) canv= new CusCanvas() ;
        curr=canv;
        dsp.setCurrent(canv);
      }

  class CusCanvas extends Canvas
     implements CommandListener {
    
    CusCanvas(){
      this.addCommand( new Command("Back", Command.BACK, 0 ) );
      this.setCommandListener(this);
    }
    protected void paint(Graphics g) {
        int w = getWidth();
        int h = getHeight();
        g.setGrayScale(255);
        g.fillRect( 0,0,w,h );
        g.setGrayScale(15*16);
        h = Math.min( w, h );
        
        long mf = 100000000;
        int ang1 = (int)(( (n1*(36000*mf/result)) +50*mf)/(100*mf)) ;
        int orig=90;
        g.fillArc(0,0,h,h, orig, (int)ang1);
        g.setGrayScale(13*16);
        g.fillArc(0,0,h,h, (int)(orig+ang1), (int)(360-ang1) );

        g.setGrayScale(0);
        g.drawString("A "+n1+"  ", h/2, h/2-10,
                Graphics.BASELINE|Graphics.RIGHT);
        g.drawString("  B "+n2, h/2, h/2-10,
                Graphics.BASELINE|Graphics.LEFT);
        g.drawString("Total "+(n1+n2), h/2, h/2,
                Graphics.TOP|Graphics.HCENTER);
    }
    public void commandAction( Command c, Displayable d) {
        curr=fm;
        dsp.setCurrent(fm);
    }
  } // end CusCanvas
  
  public void startApp() {
    dsp=Display.getDisplay(this);
    if (curr!=null)
        dsp.setCurrent(curr);
    else
    {
        dsp.setCurrent(fm);
        curr=fm;
    }
  }
  public void pauseApp() {}
  public void destroyApp(boolean b) {}
}


